import $https from "../../https/https"
import $api from "../../https/Api"
// pages/serch/serch.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer:0,
    vuale:"",
    sercList:[],
    history:[],
    historyflag:true
  }, 
  del(){
      var than=this
     than.setData({
       history:[]
     })
      wx.removeStorageSync('history')
  },
  gotoGood(e){
    console.log(e.currentTarget.dataset.spring)
    wx.navigateTo({
      url: '/pages/goods_list/goods_list?query='+e.currentTarget.dataset.spring,
    })
  },
  textclick(e){
    var than=this
    var historythan=than.data.history
    var kw=e.currentTarget.dataset.index
    // console.log(kw)
    if(historythan.indexOf(kw)!=-1){
      historythan.splice(historythan.indexOf(kw), 1);
    }
    historythan.unshift(kw)
    than.setData({
      history:historythan,
      vuale:e.currentTarget.dataset.index
    })
    wx.setStorageSync('history', JSON.stringify(than.data.history))
    than.getserchList()
  },
  clear(){
    console.log("清除")
      var than=this
     than.setData({
       vuale:[],
       sercList:[],
       historyflag:true
     })
  },
  gethistory(){
    var than=this
    var historythan=than.data.history
      console.log(than.data.vuale.length)
      if(than.data.vuale.trim()=="")
      {
        than.data.historyflag=true
        return
      }
      if(historythan.indexOf(than.data.vuale)!=-1){
        historythan.splice(historythan.indexOf(than.data.vuale), 1);
      }
      historythan.unshift(than.data.vuale)
      than.setData({
        history:historythan
      })
      wx.setStorageSync('history', JSON.stringify(than.data.history))
      console.log(than.data.history)
      
  },
  getserchList(){
    var than=this
    var qsearch=than.data.vuale
    // console.log(qsearch)
    $https.get($api.qsearch,"Get",{query:qsearch}).then((res)=>{
      // console.log(res.message)
      than.setData({
        sercList:res.message,
        historyflag:false
      })
      // console.log(than.data.sercList.length==0)
      if(than.data.sercList.length==0)
        than.data.historyflag=true
      // console.log( than.data.historyflag)
      than.gethistory()
    })
  },
  input(e){
    var than=this
    clearTimeout(than.data.timer)
    than.data.timer=setTimeout(() => {
      than.setData({
        vuale:e.detail.value
      })
      than.getserchList()
    //  than.getserchList()
  }, 1000);

},
gethistorylist(){
  var than=this
  than.setData({
    history:JSON.parse(wx.getStorageSync('history')||'[]')
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var than=this
    than.gethistorylist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})